/*
 * @Copyright: Citylife 2021
 * @Author: shaokui.gu
 * @Date: 2021-02-19 13:52:41
 * @Description: 
 */
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_background/flutter_background.dart';
import 'config/config.dart';
import 'generated/l10n.dart';
import 'routes/app_pages.dart';
import 'utils/locale/local_manager.dart';
import 'utils/sizer/sizer_util.dart';
import 'utils/theme/theme_manager.dart';

void main() {
  _initConfig();
  _configWebRTC();
  runApp(MyApp());
}

_initConfig() {
  //注册跳转页面路由
  AppPages.configureRoutes();

  /// 配置环境
  Config.env = "";
}

_configWebRTC() {
  if (WebRTC.platformIsAndroid) {
    WidgetsFlutterBinding.ensureInitialized();
    _startForegroundService();
  }
}

Future<bool> _startForegroundService() async {
  final androidConfig = FlutterBackgroundAndroidConfig(
    notificationTitle: 'Title of the notification',
    notificationText: 'Text of the notification',
    notificationImportance: AndroidNotificationImportance.Default,
    notificationIcon: AndroidResource(
        name: 'background_icon',
        defType: 'drawable'), // Default is ic_launcher from folder mipmap
  );
  return FlutterBackground.initialize(androidConfig: androidConfig);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      headerBuilder: () => WaterDropHeader(),
      // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
      footerBuilder: () => ClassicFooter(),
      // 配置默认底部指示器
      headerTriggerDistance: 80.0,
      // 头部触发刷新的越界距离
      springDescription:
          SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
      // 自定义回弹动画,三个属性值意义请查询flutter api
      maxOverScrollExtent: 100,
      //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
      maxUnderScrollExtent: 0,
      // 底部最大可以拖动的范围
      enableScrollWhenRefreshCompleted: true,
      //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
      enableLoadingWhenFailed: true,
      //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
      hideFooterWhenNotFull: false,
      // Viewport不满一屏时,禁用上拉加载更多功能
      enableBallisticLoad: true,
      // 可以通过惯性滑动触发加载更多
      child: LayoutBuilder(
        builder: (context, constraints) {
          return OrientationBuilder(
            builder: (BuildContext context, Orientation orientation) {
              SizerUtil.init(constraints, orientation);
              return GetMaterialApp(
                defaultTransition: Transition.rightToLeft,
                popGesture: GetPlatform.isIOS && !kIsWeb,
                title: 'Flutter 脚手架',
                debugShowCheckedModeBanner: false,
                localizationsDelegates: [
                  RefreshLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                  S.delegate,
                ],
                locale: LocaleManager.instance.locale,
                supportedLocales: S.delegate.supportedLocales,
                initialRoute: '/',
                getPages: AppPages.pages,
                theme: ThemeData(
                  primaryColor: theme.colors.defaultTitleColor,
                  secondaryHeaderColor: theme.colors.defaultDescColor,
                  visualDensity: VisualDensity.adaptivePlatformDensity,
                ),
                themeMode: theme.themeMode,
              );
            },
          );
        },
      ),
    );
  }
}
